package com.demo.androidgame.finsternis;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;







import android.os.CountDownTimer;
import android.os.SystemClock;

import javax.microedition.khronos.opengles.GL10;







import android.util.Log;
import android.view.VelocityTracker;
import android.view.View.OnTouchListener;

import com.demo.androidgame.fx.Game;
import com.demo.androidgame.fx.GameObject;
import com.demo.androidgame.fx.Input.TouchEvent;
import com.demo.androidgame.fx.gl.TextureRegion;
import com.demo.androidgame.fx.impl.GLGame;
import com.demo.androidgame.fx.impl.GLGraphics;
import com.demo.androidgame.fx.impl.GLScreen;
import com.demo.androidgame.fx.math.OverlapTester;
import com.demo.androidgame.fx.math.Vector2;
import com.demo.androidgame.finsternis.Platform;
import com.demo.androidgame.finsternis.DummyPlatform;
import com.demo.androidgame.finsternis.Nis;
import com.demo.androidgame.finsternis.TeleportPlatform;

public class World {	
    public interface WorldListener {
        public void jump();

        public void highJump();

        public void hit();

        public void coin();
    }

    // 가로 세로 화면 사이즈 / 32pixel 기준
    public static final float WORLD_WIDTH = 60;
    public static final float WORLD_HEIGHT = 10;

    public static final int WORLD_STATE_RUNNING = 0;
    public static final int WORLD_STATE_NEXT_LEVEL = 1;
    public static final int WORLD_STATE_GAME_OVER = 2;
    public static final Vector2 gravity = new Vector2(0, -12);		//중력 : 아래쪽으로 당기는 힘 (점프 높이가 조절됨)
    												//0, -12
    public final Nis bob;
    public final List<Platform> platforms;
    public final List<DummyPlatform> dummyplatforms;
    public Tomb tomb;
    public final List<TeleportPlatform> teleports;
    public Door door;
    
    public final WorldListener listener;

    public float heightSoFar;
    public int state;
    
    float time = 60;
    
    public final Game game;
    
    public static int stage;	//스테이지 상태

    /////////////////////
    public CountDownTimer countDown = null;    
    public long millisInFuture, countDownInterval;
    float timeInMilliseconds;
    float updatedTime;
    float timeSwapBuff;
    float startTime;
    
    /////////////////////
    
    public World(WorldListener listener, Game game) {

        this.bob = new Nis(GameScreen.bobStartX, GameScreen.bobStartY);
        this.platforms = new ArrayList<Platform>();
        this.dummyplatforms = new ArrayList<DummyPlatform>();
        this.teleports = new ArrayList<TeleportPlatform>();
        
        this.listener = listener;
        this.game = game;
        generateLevel();
        this.heightSoFar = 0;
        this.state = WORLD_STATE_RUNNING;
        
    }

	private void generateLevel() {
		
		switch(stage) {
	    case 1 :
	    	stageWorld1();
	        break;
	    case 2 :
	    	stageWorld2();
	        break;
	    case 3 :
	    	stageWorld3();
	        break;
	    case 4 :
	    	stageWorld4();
	        break;
	    case 5 :
	    	stageWorld5();
	        break;
	    case 6 :
	    	stageWorld6();
	        break;
		}
		
	}

	////////////////////////////////////////////////
	//스테이지1
	private void stageWorld1() {
		//플랫폼 생성
		int type = Platform.platform1;
	    Platform platform;
	    for (int i = 0; i < 9; i++){
	    	platform = new Platform(type, i, 0);
	        platforms.add(platform);
	    }
	    
	    for (int i = 11; i < 24; i++){
	    	platform = new Platform(type, i, 0);
	        platforms.add(platform);
	    }
	    
	    for (int i = 29; i < 39; i++){
	    	platform = new Platform(type, i, 0);
	        platforms.add(platform);
	    }
	    
	    for (int i = 49; i < 61; i++){
	    	platform = new Platform(type, i, 0);
	        platforms.add(platform);
	    }
	    
	    for (int i = 7; i < 13; i++){
	    	platform = new Platform(type, i, 2);
	        platforms.add(platform);
	    }
	    
	    for (int i = 25; i < 28; i++){
	    	platform = new Platform(type, i, 2);
	        platforms.add(platform);
	    }
	    
	    for (int i = 38; i < 42; i++){
	    	platform = new Platform(type, i, 2);
	        platforms.add(platform);
	    }
	    
	    for (int i = 55; i < 58; i++){
	    	platform = new Platform(type, i, 2);
	        platforms.add(platform);
	    }
	    for (int i = 54; i < 57; i++){
	    	platform = new Platform(type, i, 3);
	        platforms.add(platform);
	    }
	    for (int i = 53; i < 56; i++){
	    	platform = new Platform(type, i, 4);
	        platforms.add(platform);
	    }
	    for (int i = 52; i < 55; i++){
	    	platform = new Platform(type, i, 5);
	        platforms.add(platform);
	    }
	    for (int i = 50; i < 54; i++){
	    	platform = new Platform(type, i, 6);
	        platforms.add(platform);
	    }
	    for (int i = 50; i < 53; i++){
	    	platform = new Platform(type, i, 7);
	        platforms.add(platform);
	    }
	    for (int i = 50; i < 52; i++){
	    	platform = new Platform(type, i, 8);
	        platforms.add(platform);
	    }
	    
	    for (int i = 37; i < 34; i--){
	    	platform = new Platform(type, i, 4);
	        platforms.add(platform);
	    }
	    
	    for (int i = 39; i < 43; i++){
	    	platform = new Platform(type, i, 6);
	        platforms.add(platform);
	    }
	    
	    for (int i = 2; i < 5; i++){
	    	platform = new Platform(type, 44, i);
	        platforms.add(platform);
	    }
	    for (int i = 45; i < 48; i++){
	    	platform = new Platform(type, i, 4);
	        platforms.add(platform);
	    }
	    
        platform = new Platform(type, 15, 1);
        platforms.add(platform);
        platform = new Platform(type, 19, 1);
        platforms.add(platform);
        
        tomb = new Tomb(1, 1);
	    door = new Door(50, 9);
	    
	}
	
	//스테이지2
	private void stageWorld2() {
		//플랫폼 생성
		int type = Platform.platform1;
		Platform platform;
		
		for (int i = 0; i < 3; i++){
	    	platform = new Platform(type, i, 0);
	        platforms.add(platform);
	    }
		
		for (int i = 4; i < 8; i++){
	    	platform = new Platform(type, i, 1);
	        platforms.add(platform);
	    }
		
		for (int i = 10; i < 12; i++){
	    	platform = new Platform(type, i, 2);
	        platforms.add(platform);
	    }
		
		for (int i = 14; i < 17; i++){
	    	platform = new Platform(type, i, 2);
	        platforms.add(platform);
	    }
		
		for (int i = 18; i < 20; i++){
	    	platform = new Platform(type, i, 3);
	        platforms.add(platform);
	    }
		
		for (int i = 21; i < 23; i++){
	    	platform = new Platform(type, i, 4);
	        platforms.add(platform);
	    }
		
		for (int i = 24; i < 26; i++){
	    	platform = new Platform(type, i, 5);
	        platforms.add(platform);
	    }
		
		for (int i = 28; i < 32; i++){
	    	platform = new Platform(type, i, 2);
	        platforms.add(platform);
	    }
		
		for (int i = 31; i < 34; i++){
	    	platform = new Platform(type, i, 3);
	        platforms.add(platform);
	    }
		
		for (int i = 33; i < 35; i++){
	    	platform = new Platform(type, i, 4);
	        platforms.add(platform);
	    }
		
		platform = new Platform(type, 34, 5);	platforms.add(platform);
		
		for (int i = 37; i < 39; i++){
	    	platform = new Platform(type, i, 5);
	        platforms.add(platform);
	    }
		
		for (int i = 40; i < 42; i++){
	    	platform = new Platform(type, i, 6);
	        platforms.add(platform);
	    }
		
		for (int i = 44; i < 46; i++){
	    	platform = new Platform(type, i, 6);
	        platforms.add(platform);
	    }
		
		for (int i = 47; i < 49; i++){
	    	platform = new Platform(type, i, 3);
	        platforms.add(platform);
	    }
		
		for (int i = 51; i < 53; i++){
	    	platform = new Platform(type, i, 3);
	        platforms.add(platform);
	    }
		
		for (int i = 54; i < 58; i++){
	    	platform = new Platform(type, i, 1);
	        platforms.add(platform);
	    }
		
		tomb = new Tomb(1, 1);
		door = new Door(56, 2);
		
	}
	
	//스테이지3
	private void stageWorld3() {
		//플랫폼 생성
		int type = Platform.platform1;
		Platform platform;
		
		for (int i = 0; i < 2; i++){
		   	platform = new Platform(type, i, 0);
		    platforms.add(platform);
		}
				
		platform = new Platform(type, 4, 0);	platforms.add(platform);
		platform = new Platform(type, 7, 0);	platforms.add(platform);
		platform = new Platform(type, 10, 0);	platforms.add(platform);
		
		for (int i = 12; i < 15; i++){
		   	platform = new Platform(type, i, 1);
		    platforms.add(platform);
		}
		
		platform = new Platform(type, 15, 6);	platforms.add(platform);
		for (int i = 15; i < 28; i++){
		   	platform = new Platform(type, i, 5);
		    platforms.add(platform);
		}
		platform = new Platform(type, 27, 6);	platforms.add(platform);
		
		
		for (int i = 17; i < 19; i++){
		   	platform = new Platform(type, i, 1);
		    platforms.add(platform);
		}
		
		platform = new Platform(type, 21, 1);	platforms.add(platform);
		platform = new Platform(type, 24, 1);	platforms.add(platform);
		platform = new Platform(type, 27, 1);	platforms.add(platform);
		platform = new Platform(type, 30, 1);	platforms.add(platform);
		platform = new Platform(type, 33, 1);	platforms.add(platform);
		
		
		platform = new Platform(type, 32, 7);	platforms.add(platform);
		for (int i = 32; i < 43; i++){
		   	platform = new Platform(type, i, 6);
		    platforms.add(platform);
		}
		platform = new Platform(type, 42, 7);	platforms.add(platform);
		
		
		for (int i = 35; i < 37; i++){
		   	platform = new Platform(type, i, 2);
		    platforms.add(platform);
		}
		
		platform = new Platform(type, 39, 2);	platforms.add(platform);
		platform = new Platform(type, 42, 2);	platforms.add(platform);
		platform = new Platform(type, 45, 2);	platforms.add(platform);
		
		
		for (int i = 5; i < 8; i++){
		   	platform = new Platform(type, 46, i);
		    platforms.add(platform);
		}
		for (int i = 5; i < 8; i++){
		   	platform = new Platform(type, 47, i);
		    platforms.add(platform);
		}
		
		
		for (int i = 48; i < 50; i++){
		   	platform = new Platform(type, i, 2);
		    platforms.add(platform);
		}
		for (int i = 49; i < 51; i++){
		   	platform = new Platform(type, i, 3);
		    platforms.add(platform);
		}
		platform = new Platform(type, 50, 4);	platforms.add(platform);
		
		for (int i = 50; i < 52; i++){
		   	platform = new Platform(type, i, 8);
		    platforms.add(platform);
		}
		
		for (int i = 53; i < 55; i++){
		   	platform = new Platform(type, i, 4);
		    platforms.add(platform);
		}
		
		for (int i = 57; i < 59; i++){
		   	platform = new Platform(type, i, 1);
		    platforms.add(platform);
		}
		
		tomb = new Tomb(1, 1);
		door = new Door(58, 2);

	}
	
	//스테이지4
	private void stageWorld4() {
		//플랫폼 생성
		int type = Platform.platform1;
		Platform platform;
		TeleportPlatform teleport;
		DummyPlatform dummyPlatform;
		
		for (int x = 0; x < 17; x++){
	    	platform = new Platform(type, x, 7);
	        platforms.add(platform);
	    }
		platform = new Platform(type, 4, 8);	platforms.add(platform);
		platform = new Platform(type, 8, 8);	platforms.add(platform);
		platform = new Platform(type, 12, 8);	platforms.add(platform);
		for (int y = 7; y < 11; y++){
	    	platform = new Platform(type, 16, y);
	        platforms.add(platform);
	    }
		for (int x = 17; x < 21; x++){
			dummyPlatform = new DummyPlatform(type, x, 8);
			dummyplatforms.add(dummyPlatform);
	    }
		for (int x = 20; x < 24; x++){
			dummyPlatform = new DummyPlatform(type, x, 7);
			dummyplatforms.add(dummyPlatform);
	    }
		for (int x = 23; x < 26; x++){
			dummyPlatform = new DummyPlatform(type, x, 6);
			dummyplatforms.add(dummyPlatform);
	    }
		
		
		dummyPlatform = new DummyPlatform(type, 2, 6);	dummyplatforms.add(dummyPlatform);
		for (int x = 0; x < 3; x++){
	    	platform = new Platform(type, x, 5);
	        platforms.add(platform);
	    }
		for (int x = 2; x < 6; x++){
	    	platform = new Platform(type, x, 4);
	        platforms.add(platform);
	    }
		dummyPlatform = new DummyPlatform(type, 6, 4);	dummyplatforms.add(dummyPlatform);
		
		platform = new Platform(type, 8, 4);	platforms.add(platform);
		
		for (int x = 11; x < 13; x++){
	    	platform = new Platform(type, x, 4);
	        platforms.add(platform);
	    }
		
		for (int x = 15; x < 19; x++){
	    	platform = new Platform(type, x, 4);
	        platforms.add(platform);
	    }
		
		platform = new Platform(type, 21, 4);	platforms.add(platform);
		dummyPlatform = new DummyPlatform(type, 23, 4);	dummyplatforms.add(dummyPlatform);
		platform = new Platform(type, 24, 4);	platforms.add(platform);
		
		for (int x = 27; x < 29; x++){
	    	platform = new Platform(type, x, 4);
	        platforms.add(platform);
	    }
		
		dummyPlatform = new DummyPlatform(type, 32, 4);	dummyplatforms.add(dummyPlatform);
		for (int x = 31; x < 33; x++){
			dummyPlatform = new DummyPlatform(type, x, 3);
			dummyplatforms.add(dummyPlatform);
	    }
		for (int x = 30; x < 34; x++){
	    	platform = new Platform(type, x, 2);
	        platforms.add(platform);
	    }
		
		for (int x = 36; x < 41; x++){
	    	platform = new Platform(type, x, 2);
	        platforms.add(platform);
	    }
		for (int x = 40; x < 44; x++){
	    	platform = new Platform(type, x, 3);
	        platforms.add(platform);
	    }
		for (int x = 43; x < 46; x++){
	    	platform = new Platform(type, x, 4);
	        platforms.add(platform);
	    }
		for (int x = 45; x < 50; x++){
	    	platform = new Platform(type, x, 3);
	        platforms.add(platform);
	    }
		for (int x = 49; x < 51; x++){
	    	platform = new Platform(type, x, 4);
	        platforms.add(platform);
	    }
		for (int x = 50; x < 52; x++){
			dummyPlatform = new DummyPlatform(type, x, 3);
			dummyplatforms.add(dummyPlatform);
	    }
		dummyPlatform = new DummyPlatform(type, 51, 4);	dummyplatforms.add(dummyPlatform);
		
		for (int x = 54; x < 56; x++){
	    	platform = new Platform(type, x, 3);
	        platforms.add(platform);
	    }
		for (int x = 55; x < 58; x++){
	    	platform = new Platform(type, x, 4);
	        platforms.add(platform);
	    }
		
		for (int x = 43; x < 61; x++){
	    	platform = new Platform(type, x, 1);
	        platforms.add(platform);
	    }
		for (int y = 2; y < 8; y++){
	    	platform = new Platform(type, 60, y);
	        platforms.add(platform);
	    }
		
		teleport = new TeleportPlatform(0, 43, 2);	teleports.add(teleport);
		teleport = new TeleportPlatform(1, 14, 8);	teleports.add(teleport);
		
//		for (int i = 0; i < 20; i++){
//	    	dummyPlatform = new DummyPlatform(type, i, 4);
//	        dummyplatforms.add(dummyPlatform);
//	    }
		
		
		tomb = new Tomb(1, 6);	//1,5
		door = new Door(1, 8);
		
	}
	
	//스테이지5
	private void stageWorld5() {
		//플랫폼 생성
		int type = Platform.platform1;
		Platform platform;
		TeleportPlatform teleport;
		DummyPlatform dummyPlatform;
				
		for (int x = 0; x < 2; x++) {
			platform = new Platform(type, x, 3);
			platforms.add(platform);
		}
		
		for (int x = 3; x < 6; x++) {
			platform = new Platform(type, x, 0);
			platforms.add(platform);
		}
		for (int x = 5; x < 7; x++) {
			platform = new Platform(type, x, 1);
			platforms.add(platform);
		}
		for (int x = 6; x < 8; x++) {
			platform = new Platform(type, x, 2);
			platforms.add(platform);
		}
		
		for (int x = 10; x < 13; x++) {
			platform = new Platform(type, x, 0);
			platforms.add(platform);
		}
		platform = new Platform(type, 12, 1);	platforms.add(platform);
		
		for (int y = 0; y < 2; y++) {
			platform = new Platform(type, 14, y);
			platforms.add(platform);
		}
		platform = new Platform(type, 16, 0);	platforms.add(platform);
		
		for (int y = 0; y < 2; y++) {
			platform = new Platform(type, 18, y);
			platforms.add(platform);
		}
		platform = new Platform(type, 20, 0);	platforms.add(platform);
		
		for (int y = 0; y < 2; y++) {
			platform = new Platform(type, 22, y);
			platforms.add(platform);
		}
		platform = new Platform(type, 24, 0);	platforms.add(platform);
		
		for (int y = 0; y < 2; y++) {
			platform = new Platform(type, 26, y);
			platforms.add(platform);
		}
		
		for (int y = 0; y < 10; y++) {
			platform = new Platform(type, 29, y);
			platforms.add(platform);
		}
		
		
		
		for (int x = 0; x < 29; x++){
			dummyPlatform = new DummyPlatform(type, x, 5);
			dummyplatforms.add(dummyPlatform);
	    }
		
		for (int x = 0; x < 2; x++) {
			platform = new Platform(type, x, 8);
			platforms.add(platform);
		}
		for (int x = 1; x < 4; x++) {
			platform = new Platform(type, x, 7);
			platforms.add(platform);
		}
		
		for (int x = 5; x < 26; x=x+2) {
			platform = new Platform(type, x, 7);
			platforms.add(platform);
		}
		dummyPlatform = new DummyPlatform(type, 26, 7);	dummyplatforms.add(dummyPlatform);
		platform = new Platform(type, 27, 6);	platforms.add(platform);
		dummyPlatform = new DummyPlatform(type, 28, 7);	dummyplatforms.add(dummyPlatform);

		
		
		for (int x = 30; x < 34; x++) {
			platform = new Platform(type, x, 0);
			platforms.add(platform);
		}
		
		for (int x = 35; x < 37; x++) {
			platform = new Platform(type, x, 0);
			platforms.add(platform);
		}
		for (int x = 36; x < 39; x++) {
			platform = new Platform(type, x, 1);
			platforms.add(platform);
		}
		for (int x = 38; x < 40; x++) {
			platform = new Platform(type, x, 0);
			platforms.add(platform);
		}
	
		for (int x = 41; x < 48; x=x+2) {
			platform = new Platform(type, x, 0);
			platforms.add(platform);
		}
		
		for (int x = 49; x < 51; x++) {
			platform = new Platform(type, x, 0);
			platforms.add(platform);
		}
		for (int x = 53; x < 55; x++) {
			platform = new Platform(type, x, 0);
			platforms.add(platform);
		}
		for (int x = 57; x < 60; x++) {
			platform = new Platform(type, x, 0);
			platforms.add(platform);
		}
		
		
		
		for (int x = 30; x < 61; x++){
			dummyPlatform = new DummyPlatform(type, x, 3);
			dummyplatforms.add(dummyPlatform);
	    }
		
		for (int x = 31; x < 33; x++) {
			platform = new Platform(type, x, 5);
			platforms.add(platform);
		}
		
		for (int x = 35; x < 38; x++) {
			platform = new Platform(type, x, 5);
			platforms.add(platform);
		}
		
		for (int y = 5; y < 7; y++) {
			platform = new Platform(type, 39, y);
			platforms.add(platform);
		}
		for (int y = 4; y < 6; y++) {
			platform = new Platform(type, 41, y);
			platforms.add(platform);
		}
		for (int y = 5; y < 7; y++) {
			platform = new Platform(type, 43, y);
			platforms.add(platform);
		}
		for (int y = 6; y < 8; y++) {
			platform = new Platform(type, 45, y);
			platforms.add(platform);
		}
		
		for (int x = 47; x < 49; x++) {
			platform = new Platform(type, x, 7);
			platforms.add(platform);
		}
		
		for (int y = 5; y < 7; y++) {
			platform = new Platform(type, 50, y);
			platforms.add(platform);
		}
		for (int y = 5; y < 7; y++) {
			platform = new Platform(type, 52, y);
			platforms.add(platform);
		}
		
		for (int x = 54; x < 59; x=x+2) {
			platform = new Platform(type, x, 6);
			platforms.add(platform);
		}
		
		
		teleport = new TeleportPlatform(0, 58, 1);	teleports.add(teleport);
		teleport = new TeleportPlatform(1, 0, 4);	teleports.add(teleport);
		
		teleport = new TeleportPlatform(0, 26, 2);	teleports.add(teleport);
		teleport = new TeleportPlatform(1, 31, 6);	teleports.add(teleport);
		
		teleport = new TeleportPlatform(0, 58, 7);	teleports.add(teleport);
		teleport = new TeleportPlatform(1, 1, 9);	teleports.add(teleport);
		
		tomb = new Tomb(31, 1);
		door = new Door(27, 7);
	}
	
	//스테이지6
	private void stageWorld6() {
		//플랫폼 생성
		int type = Platform.platform1;
		Platform platform;
		TeleportPlatform teleport;
		DummyPlatform dummyPlatform;
		
		for (int x = 0; x < 16; x++) {
			platform = new Platform(type, x, 0);
			platforms.add(platform);
		}
		for (int x = 7; x < 10; x++) {
			platform = new Platform(type, x, 1);
			platforms.add(platform);
		}
		for (int x = 8; x < 10; x++) {
			platform = new Platform(type, x, 2);
			platforms.add(platform);
		}
		for (int x = 9; x < 17; x++) {
			platform = new Platform(type, x, 3);
			platforms.add(platform);
		}
		platform = new Platform(type, 14, 4);	platforms.add(platform);
		for (int y = 1; y < 3; y++) {
			platform = new Platform(type, 15, y);
			platforms.add(platform);
		}
		for (int y = 4; y < 7; y++) {
			platform = new Platform(type, 15, y);
			platforms.add(platform);
		}
		for (int y = 6; y < 8; y++) {
			platform = new Platform(type, 16, y);
			platforms.add(platform);
		}
		for (int x = 16; x < 18; x++) {
			platform = new Platform(type, x, 8);
			platforms.add(platform);
		}
		dummyPlatform = new DummyPlatform(type, 18, 8);		dummyplatforms.add(dummyPlatform);
	    platform = new Platform(type, 19, 8);	platforms.add(platform);
	    for (int x = 20; x < 23; x++){
			dummyPlatform = new DummyPlatform(type, x, 8);
			dummyplatforms.add(dummyPlatform);
	    }
	    platform = new Platform(type, 23, 8);	platforms.add(platform);
	    for (int x = 24; x < 26; x++){
			dummyPlatform = new DummyPlatform(type, x, 8);
			dummyplatforms.add(dummyPlatform);
	    }
	    platform = new Platform(type, 26, 8);	platforms.add(platform);
	    for (int x = 27; x < 29; x++){
			dummyPlatform = new DummyPlatform(type, x, 8);
			dummyplatforms.add(dummyPlatform);
	    }
	    
	    platform = new Platform(type, 21, 4);	platforms.add(platform);
	    for (int x = 22; x < 24; x++){
			dummyPlatform = new DummyPlatform(type, x, 4);
			dummyplatforms.add(dummyPlatform);
	    }
	    platform = new Platform(type, 24, 4);	platforms.add(platform);
	    for (int x = 25; x < 27; x++){
			dummyPlatform = new DummyPlatform(type, x, 4);
			dummyplatforms.add(dummyPlatform);
	    }
	    platform = new Platform(type, 27, 4);	platforms.add(platform);
	    
	    
	    for (int y = 0; y < 9; y++) {
			platform = new Platform(type, 29, y);
			platforms.add(platform);
		}
	    platform = new Platform(type, 28, 2);	platforms.add(platform);
	    for (int x = 16; x < 29; x++){
			dummyPlatform = new DummyPlatform(type, x, 0);
			dummyplatforms.add(dummyPlatform);
	    }
	    
	    
	    for (int x = 30; x < 32; x++){
			dummyPlatform = new DummyPlatform(type, x, 8);
			dummyplatforms.add(dummyPlatform);
	    }
	    platform = new Platform(type, 32, 8);	platforms.add(platform);
	    for (int x = 33; x < 35; x++){
			dummyPlatform = new DummyPlatform(type, x, 8);
			dummyplatforms.add(dummyPlatform);
	    }
	    platform = new Platform(type, 35, 8);	platforms.add(platform);
	    for (int x = 36; x < 38; x++){
			dummyPlatform = new DummyPlatform(type, x, 8);
			dummyplatforms.add(dummyPlatform);
	    }
	    
	    
	    for (int x = 38; x < 40; x++) {
			platform = new Platform(type, x, 5);
			platforms.add(platform);
		}
	    for (int y = 2; y < 8; y++) {
			platform = new Platform(type, 40, y);
			platforms.add(platform);
		}
	    for (int y = 2; y < 15; y++) {
			platform = new Platform(type, 41, y);
			platforms.add(platform);
		}
	    
	    
	    
	    for (int x = 30; x < 38; x++){
			dummyPlatform = new DummyPlatform(type, x, 0);
			dummyplatforms.add(dummyPlatform);
	    }
	    platform = new Platform(type, 38, 8);	platforms.add(platform);
	    
	    
	    for (int x = 38; x < 55; x++) {
			platform = new Platform(type, x, 0);
			platforms.add(platform);
		}
	    
	    
	    platform = new Platform(type, 47, 4);	platforms.add(platform);
	    
	    for (int x = 45; x < 47; x++){
			dummyPlatform = new DummyPlatform(type, x, 7);
			dummyplatforms.add(dummyPlatform);
	    }
	    dummyPlatform = new DummyPlatform(type, 46, 6);		dummyplatforms.add(dummyPlatform);
	    for (int y = 6; y < 8; y++) {
			platform = new Platform(type, 47, y);
			platforms.add(platform);
		}
	    
	    
	    for (int y = 2; y < 10; y++) {
			platform = new Platform(type, 50, y);
			platforms.add(platform);
		}
	    dummyPlatform = new DummyPlatform(type, 50, 1);		dummyplatforms.add(dummyPlatform);
	    
	    
	    for (int y = 1; y < 7; y++) {
			platform = new Platform(type, 53, y);
			platforms.add(platform);
		}
	    for (int y = 1; y < 7; y++) {
			platform = new Platform(type, 54, y);
			platforms.add(platform);
		}
	    for (int y = 0; y < 7; y++) {
	    	dummyPlatform = new DummyPlatform(type, 55, y);
	    	dummyplatforms.add(dummyPlatform);
		}
	    for (int x = 56; x < 60; x++){
			dummyPlatform = new DummyPlatform(type, x, 6);
			dummyplatforms.add(dummyPlatform);
	    }
	    
	    
	    dummyPlatform = new DummyPlatform(type, 56, 0);		dummyplatforms.add(dummyPlatform);
	    for (int x = 57; x < 60; x++) {
			platform = new Platform(type, x, 0);
			platforms.add(platform);
		}
	    for (int y = 0; y < 10; y++) {
			platform = new Platform(type, 60, y);
			platforms.add(platform);
		}
	    
	    
		teleport = new TeleportPlatform(0, 16, 4);	teleports.add(teleport);
		teleport = new TeleportPlatform(1, 59, 9);	teleports.add(teleport);
		
		teleport = new TeleportPlatform(0, 51, 3);	teleports.add(teleport);
		teleport = new TeleportPlatform(1, 48, 7);	teleports.add(teleport);
		
		teleport = new TeleportPlatform(0, 47, 5);	teleports.add(teleport);
		teleport = new TeleportPlatform(1, 35, 7);	teleports.add(teleport);
		
		teleport = new TeleportPlatform(0, 9, 8);	teleports.add(teleport);
		teleport = new TeleportPlatform(1, 4, 7);	teleports.add(teleport);
		
		teleport = new TeleportPlatform(0, 0, 5);	teleports.add(teleport);
		teleport = new TeleportPlatform(1, 3, 9);	teleports.add(teleport);
		
		teleport = new TeleportPlatform(0, 0, 8);	teleports.add(teleport);
		teleport = new TeleportPlatform(1, 14, 2);	teleports.add(teleport);
		
		
		tomb = new Tomb(27, 5);
		door = new Door(10, 1);
	}
	
	////////////////////////////////////////////////

	public void update(float deltaTime, float accelX) {
	    updateBob(deltaTime, accelX);
	    updatePlatforms(deltaTime);
	    updateDummyPlatforms(deltaTime);
	    checkCollisions();
	    checkGameOver();

	    if (time > 0) {
	    	time = time - deltaTime;
	    	
	        System.out.printf("time : [%f]\n", time);
	        checkGameOver();
	    }
	    
	    
		//Light 처리
		float[] lightAmbient = { 0.5f, 0.5f, 0.5f, 1.0f };
		float[] lightDiffuse = { 1.0f, 1.0f, 1.0f, 1.0f };
		float[] lightPosition1 = { bob.position.x, bob.position.y, 1.0f, 1.0f };
		float[] lightPosition2 = { door.position.x, door.position.y, 0.8f, 1.0f };
		  	
		GL10 gl = ((GLGame)game).getGLGraphics().getGL();
	    gl.glLightfv(GL10.GL_LIGHT1, GL10.GL_AMBIENT, lightAmbient, 0);
		gl.glLightfv(GL10.GL_LIGHT1, GL10.GL_DIFFUSE, lightDiffuse, 0);
		gl.glLightfv(GL10.GL_LIGHT1, GL10.GL_POSITION, lightPosition1, 0);
			
		gl.glEnable(GL10.GL_LIGHT1);
			
		gl.glLightfv(GL10.GL_LIGHT2, GL10.GL_AMBIENT, lightAmbient, 0);
		gl.glLightfv(GL10.GL_LIGHT2, GL10.GL_DIFFUSE, lightDiffuse, 0);
		gl.glLightfv(GL10.GL_LIGHT2, GL10.GL_POSITION, lightPosition2, 0);
			
		gl.glEnable(GL10.GL_LIGHT2);
		gl.glEnable(GL10.GL_LIGHTING);
	    
	}

	
	private void updateBob(float deltaTime, float accelX) {
	    bob.velocity.x = -accelX / 10 * Nis.BOB_MOVE_VELOCITY;		//x값의 변화를 줌
	    bob.update(deltaTime);
	    heightSoFar = Math.max(bob.position.y, heightSoFar);
	    
	    
	}

	private void updatePlatforms(float deltaTime) {
	    int len = platforms.size();
	    for (int i = 0; i < len; i++) {
	        Platform platform = platforms.get(i);
	        platform.update(deltaTime);
	    }
	}
	private void updateDummyPlatforms(float deltaTime) {
	    int len = dummyplatforms.size();
	    for (int i = 0; i < len; i++) {
	    	DummyPlatform dummyplatform = dummyplatforms.get(i);
	        dummyplatform.update(deltaTime);
	    }
	}
	
    private void checkCollisions() {
	    checkPlatformCollisions();
	    checkTeleportCollisions();
	    checkTombCollisions();
	    checkDoorCollisions();
	}
    
    private void checkTeleportCollisions() {
    	int len = teleports.size();
    	for (int i = 0; i < len; i++) {
    		TeleportPlatform teleport = teleports.get(i);
    		if (OverlapTester.overlapRectangles(bob.bounds, teleport.bounds)) {
    			if (teleport.type != teleport.TELEPORT_TYPE_OUT) {
    				bob.position.x = teleports.get(i+1).position.x;
    				bob.position.y = teleports.get(i+1).position.y;
    				Assets.playSound(Assets.teleportSound);
    				break;
    			}
    		}
    	}
    
	}

	private void checkPlatformCollisions() {
//	    if (bob.velocity.y > 0)
//	        return;    	
	    int len = platforms.size();
	    Vector2 collisionPosition = new Vector2();
	    outer1 : for (int i = 0; i < len; i++) {
	        Platform platform = platforms.get(i);
	        	//밥과 플랫폼 충돌체크
	        if (OverlapTester
	                    .overlapRectangles(bob.bounds, platform.bounds)) {
	            	
	            	float hgap = Math.abs(bob.position.x - platform.position.x);
	            	float vgap = Math.abs(bob.position.y - platform.position.y);
	            	
	            	boolean horizontalCollision = (vgap < hgap);
	            	
	            	if (horizontalCollision) {   		
	            		if (bob.position.x >= platform.position.x) {
		            		bob.position.x = platform.position.x + platform.bounds.width;
		            		continue outer1;
		            	} else {
		            		bob.position.x = platform.position.x - bob.bounds.width;
		            		continue outer1;
		            	} 
	            		
	            	} else {
	            		for (int j = 0; j < platforms.size(); j++) {
	            			Platform temp = platforms.get(j);
	            			
	            			if (temp.position.y != platform.position.y && temp.position.x == platform.position.x) {
	            				if ((bob.position.y > platform.position.y && temp.position.y == platform.position.y + Platform.PLATFORM_HEIGHT) ||
	            					(bob.position.y < platform.position.y && temp.position.y < platform.position.y - Platform.PLATFORM_HEIGHT))	
	            					continue outer1;
	            			}           		
	            		}
	            		
	            		if (bob.position.y > platform.position.y) {
		            		bob.position.y = platform.position.y + platform.bounds.height;
		            		bob.velocity.y = 0;
		            	} else {
		            		bob.position.y = platform.position.y - bob.bounds.height;
		            	}
	            	}
	            	
	            	collisionPosition.x = platform.position.x;
	            	collisionPosition.y = platform.position.y;	            	
	            	
	            	if (GameScreen.getJump == true){
		            	listener.jump();
		                bob.hitPlatform();
		                continue outer1;
	            	}
	                //break;
	            }
    	}
	    outer2 : for (int i = 0; i < len; i++) {
	        Platform platform = platforms.get(i);
	        	//밥과 플랫폼 충돌체크
	        if (OverlapTester
	                    .overlapRectangles(bob.bounds, platform.bounds)) {
	            	
	            	float hgap = Math.abs(bob.position.x - platform.position.x);
	            	float vgap = Math.abs(bob.position.y - platform.position.y);
	            	
	            	boolean horizontalCollision = (vgap > hgap);
	            	
	            	if (horizontalCollision) {
	            		
	            		if (bob.position.y > platform.position.y) {
		            		bob.position.y = platform.position.y + platform.bounds.height;
		            		bob.velocity.y = 0;
		            	} else {
		            		bob.position.y = platform.position.y - bob.bounds.height;
		            	} 
	            		
	            	} else {
	            		for (int j = 0; j < platforms.size(); j++) {
	            			Platform temp = platforms.get(j);
	            			
	            			if (temp.position.y != platform.position.y && temp.position.x == platform.position.x) {
	            				if ((bob.position.y > platform.position.y && temp.position.y == platform.position.y + Platform.PLATFORM_HEIGHT) ||
	            					(bob.position.y < platform.position.y && temp.position.y < platform.position.y - Platform.PLATFORM_HEIGHT))	
	            					continue outer2;
	            			}           		
	            		}

	            		if (bob.position.x >= platform.position.x) {
		            		bob.position.x = platform.position.x + platform.bounds.width;
		            		continue outer2;
		            	} else {
		            		bob.position.x = platform.position.x - bob.bounds.width;
		            		continue outer2;
		            	}
	            		
	            	}
	            	
	            	collisionPosition.x = platform.position.x;
	            	collisionPosition.y = platform.position.y;	            	
	            	
	            	if (GameScreen.getJump == true){
		            	listener.jump();
		                bob.hitPlatform();
		                continue outer2;
	            	}
	                //break;
	            }
    	}
	    GameScreen.getJump = false;
	}
    
    private void checkTombCollisions() {
    	if (OverlapTester.overlapRectangles(tomb.bounds, bob.bounds)) {
 	        
 	    }
    }
    
    private void checkDoorCollisions() {
    	if (OverlapTester.overlapRectangles(door.bounds, bob.bounds)) {
    		state = WORLD_STATE_NEXT_LEVEL;
    		Assets.playSound(Assets.doorSound);
 	    }
    }
    
    private void checkGameOver() {
        if (bob.position.y <= 0 || time <= 0.0f) {
            state = WORLD_STATE_GAME_OVER;
			Assets.playSound(Assets.gameoverSound);
			
        }
        
    }
   
   
}